home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 March / PCWorld_2007-03_cd.bin / domacnost a kancelar / scribus / scribus-1.3.3.7-win32-install.exe / lib / encodings / utf_16.py < prev    next >
Text File  |  2005-03-15  |  2KB  |  57 lines

  1. """ Python 'utf-16' Codec
  2.  
  3.  
  4. Written by Marc-Andre Lemburg (mal@lemburg.com).
  5.  
  6. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  7.  
  8. """
  9. import codecs, sys
  10.  
  11. ### Codec APIs
  12.  
  13. encode = codecs.utf_16_encode
  14.  
  15. def decode(input, errors='strict'):
  16.     return codecs.utf_16_decode(input, errors, True)
  17.  
  18. class StreamWriter(codecs.StreamWriter):
  19.     def __init__(self, stream, errors='strict'):
  20.         self.bom_written = False
  21.         codecs.StreamWriter.__init__(self, stream, errors)
  22.  
  23.     def encode(self, input, errors='strict'):
  24.         self.bom_written = True
  25.         result = codecs.utf_16_encode(input, errors)
  26.         if sys.byteorder == 'little':
  27.             self.encode = codecs.utf_16_le_encode
  28.         else:
  29.             self.encode = codecs.utf_16_be_encode
  30.         return result
  31.  
  32. class StreamReader(codecs.StreamReader):
  33.  
  34.     def reset(self):
  35.         codecs.StreamReader.reset(self)
  36.         try:
  37.             del self.decode
  38.         except AttributeError:
  39.             pass
  40.  
  41.     def decode(self, input, errors='strict'):
  42.         (object, consumed, byteorder) = \
  43.             codecs.utf_16_ex_decode(input, errors, 0, False)
  44.         if byteorder == -1:
  45.             self.decode = codecs.utf_16_le_decode
  46.         elif byteorder == 1:
  47.             self.decode = codecs.utf_16_be_decode
  48.         elif consumed>=2:
  49.             raise UnicodeError,"UTF-16 stream does not start with BOM"
  50.         return (object, consumed)
  51.  
  52. ### encodings module API
  53.  
  54. def getregentry():
  55.  
  56.     return (encode,decode,StreamReader,StreamWriter)
  57.